package com.ruoyi.web.controller.sys.workHour;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.BusiDeptWorkhour;
import com.ruoyi.system.domain.BusiUserWorkhour;
import com.ruoyi.system.domain.WorkHourAndPerson;
import com.ruoyi.system.service.BusiDeptWorkhourService;
import com.ruoyi.system.service.BusiUserWorkhourService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.countDay.webToDays;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Api("工时管理")
@RestController
@RequestMapping("/sys/WorkHour")
public class workHourController extends BaseController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    BusiUserWorkhourService busiUserWorkhourService;

    @Autowired
    BusiDeptWorkhourService busiDeptWorkhourService;

    @ApiOperation("显示所有员工")
    @PostMapping("/showAll")
    public TableDataInfo showAll() {
        startPage();
        return getDataTable(busiUserWorkhourService.showAllBusiUserWorkhour());
    }


    @ApiOperation("【只用在年初才会使用一次】一键初始化全年所有人，所有组的工时")
    @Log(title = "【警告！！】一键初始化全年所有人", businessType = BusinessType.UPDATE)
    @PostMapping("/InitWorkHour")
    public AjaxResult initWorkDays() {
        SysUser user = new SysUser();
        //获取全体员工的信息
        List<SysUser> sysUsers = userService.selectUserList(user);
        webToDays webToDays = new webToDays();
        //获取当前年份
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        //通过外部接口得到全年的法定节假日
        webToDays.getYear(year);
        ArrayList<BusiUserWorkhour> busiUserWorkhours = new ArrayList<>();
        // 获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");
        Date date = new Date();
        //取小数点后两位
        DecimalFormat df = new DecimalFormat("#.00");
        for (SysUser sysUser : sysUsers) {
            for (int i = 1; i <= 12; i++) {
                int num = webToDays.count(2020, i);
                if (num != 0) {
                    String id = IdUtils.simpleUUID();
                    BusiUserWorkhour busiUserWorkhour = new BusiUserWorkhour();
                    //封装每个人每个月的BusiUserWorkhour类
                    busiUserWorkhour.setId(id);
                    busiUserWorkhour.setUserId(sysUser.getUserId());
                    busiUserWorkhour.setUserName(sysUser.getUserName());
                    busiUserWorkhour.setTargetYear(String.valueOf(year));
                    busiUserWorkhour.setTargetMonth(String.valueOf(i));
                    busiUserWorkhour.setTargetMonthday(num);
                    busiUserWorkhour.setTargetWorkhours(Double.valueOf(df.format(sysUser.getEnterprises() * num)));
                    busiUserWorkhour.setCreateTime(date);
                    //把每个人的信息加入list
                    busiUserWorkhours.add(busiUserWorkhour);
                }
            }
        }
        for (BusiUserWorkhour busiUserWorkhour : busiUserWorkhours) {
            //添加至数据库
            busiUserWorkhourService.addBusiUserWorkhour(busiUserWorkhour);
        }
        //同步到分组工时的数据库
        List<BusiDeptWorkhour> busiDeptWorkhour = busiUserWorkhourService.getBusiDeptWorkhour();
        for (BusiDeptWorkhour deptWorkhour : busiDeptWorkhour) {
            //生成随机id
            String id = IdUtils.simpleUUID();
            deptWorkhour.setId(id);
            deptWorkhour.setTargetWorkhours(Double.valueOf(df.format(deptWorkhour.getTargetWorkhours())));
            busiDeptWorkhourService.insert(deptWorkhour);
        }
        return AjaxResult.success(busiDeptWorkhour);
    }

    /**
     * 展示全体员工所有月份的阅读基准值
     *
     * @return
     */
    @ApiOperation("显示所有人的月度基准值")
    @GetMapping("/showWorkHour")
    public TableDataInfo showWorkDays() {
        //开启分页
        startPage();
        return getDataTable(busiUserWorkhourService.showAllBusiUserWorkhour());
    }

    /**
     * 通过选择姓名来获取全年的月度基准值
     *
     * @return
     */
    @ApiOperation("通过选择姓名来获取全年的月度基准值")
    @PostMapping("/selectByName")
    public AjaxResult selectById(@ApiParam(name = "WorkHourAndPerson") @RequestBody WorkHourAndPerson workHourAndPerson) {
        return AjaxResult.success(busiUserWorkhourService.selectByName(workHourAndPerson.getPersonName()));
    }

    /**
     * 多条件查询员工基准值
     *
     * @param targetMonth
     * @param personGroup
     * @param personName
     * @return
     */
    @ApiOperation(value = "多条件模糊查询员工月度基准值")
    @GetMapping("/find")
    public TableDataInfo find(@ApiParam(name = "targetMonth" , value = "输入月份，可以为空" , required = false) @RequestParam(value = "targetMonth" , required = false) String targetMonth,
                              @ApiParam(name = "personGroup" , value = "选择部门名称，必填" , required = true) @RequestParam(value = "personGroup" , required = false) String personGroup,
                              @ApiParam(name = "personName" , value = "员工名称" , required = false) @RequestParam(value = "personName" , required = false) String personName
    ) {
        startPage();
        return getDataTable(busiUserWorkhourService.find(targetMonth, personGroup, personName));
    }

    /**
     * 员工基准值的修改
     *
     * @param workHourAndPerson
     * @return
     */
    @ApiOperation(value = "修改员工基准值")
    @PostMapping("/update")
    public AjaxResult update(@ApiParam(name = "" , value = "传入该员工的WorkHourAndGroup信息" , required = true)
                             @RequestBody WorkHourAndPerson workHourAndPerson) {
        //查找对应的BusiUserWorkhour类
        BusiUserWorkhour busiUserWorkhour = busiUserWorkhourService.selectById(workHourAndPerson.getId());
        //植入更新的内容
        busiUserWorkhour.setTargetWorkhours(workHourAndPerson.getTargetWorkhours());
        busiUserWorkhour.setRemarks(workHourAndPerson.getRemarks());
        //执行添加操作
        return AjaxResult.success(busiUserWorkhourService.update(busiUserWorkhour));
    }
}
